PHP-Tester

A very simple testing package for PHP, intended for use through web requests, instead of through CLI.

Basics

  1. Extend \Taeluf\Tester
  2. (optional) Override prepare for code to run BEFORE your tests
  3. (optional) Override showResults($details) to customize output
  4. Write tests: function testNameOfTest(){}. Have it return true or false
  5. Call YourCustomTester::runAll()

Demonstration

To execute the tester class below:

    \RTester::runAll();

And the example test class:

class RTester extends \Taeluf\Tester {

    public function prepare(){
        $this->candy = "candy";
    }

    public function testWithError(){
        echo 'disabled this test';
        throw 'blah';
        return false;
    }
    public function testForCandy(){
        echo "pointless candy printing";
        return $this->candy=="candy";
    }
}

will output something like (though with additional styling that you can't see on GitHub):

WithError: error
disabled this test

Error: Can only throw objects in /path/to/Tester.php:8 Stack trace: #0 /path/to/Tester.php:6 RTester->testWithError() #1 ... #2 ... etc
ForCandy: success
pointless candy printing
AND it is properly indented, so you can also view source and make sense of it:
<details>
    <summary><b>WithError:</b> <strong style="color:blue;">error</strong>    </summary>
    <div style="padding-left:4ch;">
        disabled this test
    </div>
    <br>
    <div style="color:red;padding-left:4ch;">        
        Error: Can only throw objects in /path/to/Tester.php:8
        Stack trace:
        #0 /path/to/Tester.php:6 RTester-&gt;testWithError()
        #1 ...
        #2 ... etc
    </div>
</details>
<details>
    <summary><b>ForCandy:</b> <strong style="color:green;">success</strong>    </summary>
    <div style="padding-left:4ch;">
        pointless candy printing
    </div>
</details>

All the ways to run tests

Call

  • MyTester::runAll()
  • $tester = new MyTester(); $tester->run(); //as an object
  • MyTester::runTests('testCamelCasedFunction'); //run one test
  • MyTester::runTests(['testOne','testTwo','testThree']); //run an array of tests

Install

  • composer require taeluf/unit-tester
  • Download/copy src/Tester.php & require it on your server
  • CLI: git clone git@github.com:ReedOverflow/PHP-Tester.git; then in php: require('/clone/dir/src/Tester.php');

Custom Results Output

You can override showResults($details) to customize output of each test. The $details array has the following keys:

  • method => The method name (except with 'test') removed
  • result => true or false (boolean)
  • return => The actual value returned by your test method
  • output => Any output from the execution of your test
  • error => a throwable or null